<?php
	include "globals.php";
	$mpq=$db->query("SELECT * FROM houses WHERE hWILL={$ir['maxwill']}

");
$mp=$db->fetch_row($mpq);
$_GET['property']=abs((int) $_GET['property']);

if($_GET['property']){
	$npq=$db->query("SELECT * FROM houses WHERE hID={$_GET['property']}

");
$np=$db->fetch_row($npq);

if($np['hWILL'] < $mp['hWILL']){
	print "You cannot go backwards in houses!";
} else
if ($np['hPRICE'] > $ir['money']){
	print "You do not have enough money to buy the {$np['hrNAME']}

.";
} else{
	$db->query("UPDATE users SET money=money-{$np['hPRICE']}

,will=0,maxwill={$np['hWILL']}

 WHERE userid=$userid");
print "Congrats, you bought the {$np['hNAME']}

 for \${$np['hPRICE']}

!";
}

} else
if (isset($_GET['sellhouse'])){
	$npq=$db->query("SELECT * FROM houses WHERE hWILL={$ir['maxwill']}

");
$np=$db->fetch_row($npq);

if($ir['maxwill'] == 100){
	print "You already live in the lowest property!";
} else{
	$db->query("UPDATE users SET money=money+{$np['hPRICE']}

,will=0,maxwill=100 WHERE userid=$userid");
print "You sold your {$np['hNAME']}

 and went back to your shed.";
}

} else{
	print "Your current property: <b>{$mp['hNAME']}

</b><br />
The houses you can buy are listed below. Click a house to buy it.<br />";

if($ir['maxwill'] > 100){
	print "<a href='estate.php?sellhouse'>Sell Your House</a><br />";
}

$hq=$db->query("SELECT * FROM houses WHERE hWILL>{$ir['maxwill']}

 ORDER BY hWILL ASC");
while($r=$db->fetch_row($hq)){
	print "<a href='estate.php?property={$r['hID']}

'>{$r['hNAME']}

</a>&nbsp;&nbsp - Cost: \${$r['hPRICE']}

&nbsp;&nbsp - Will Bar: {$r['hWILL']}

<br />";
}

}

$h->endpage();
?>
